/**
  ******************************************************************************
  * @author     Chris
  * @since      2024/6/14 22:30
  *
  * @file       hd_key.c
  * @brief      KEY Hardware Driver.
  *
  * @note       This file contains the hardware driver for the KEY.
  *
  * @warning    None.
  ******************************************************************************
  * Change Logs:
  *   Date           Author       Notes
  *   2024/6/14      Chris        the first version
  *
  ******************************************************************************
  */

#include "delay.h"
#include "hd_key.h"
#include "stm32f4xx_ll_gpio.h"


static void Key_init_(const Key* this) {
    GPIO_init(this->gpio);
}

static uint8_t Key_getNum_(const Key* this) {
    uint8_t keyNum = 0;

    if (GPIO_get(this->gpio) != RESET) {
        delay_ms(20);
        // wait for key press up
        while (GPIO_get(this->gpio) != RESET);
        delay_ms(20);
        keyNum = 1;
    }

    return keyNum;
}


void Key_constructor(Key* this, GPIO* gpio) {
    gpio->Mode = LL_GPIO_MODE_INPUT;
    gpio->Speed = LL_GPIO_SPEED_FREQ_HIGH;

    static struct KeyVtbl vtbl = {
        .init = &Key_init_,
        .get_num = &Key_getNum_,
    };

    this->vptr = &vtbl;
    this->gpio = gpio;
}
